package com.kgc.member.memberservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.member.bean.User;
import com.kgc.member.bean.UserExample;
import com.kgc.member.memberservice.mapper.UserMapper;
import com.kgc.member.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public Map<String, Object> doLogin(String name, String password) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        Map<String,Object> map=new HashMap<>();
       if (!"".equals(name)){
           criteria.andNameEqualTo(name);
           List<User> users = userMapper.selectByExample(example);
           if (users.size()>0&&users!=null){
               User user = users.get(0);
               if (password.equals(user.getPassword())){
                    if (user.getStatus()==0){
                        map.put("msg","账号已冻结");
                    }else {
                        map.put("msg",user);
                    }
               }else {
                   map.put("msg","密码输入错误!");
               }
           }else
           {
               map.put("msg","用户名不存在!");
           }
       }
        return map;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> selectUser(String name, Integer pageNum) {
        PageHelper.startPage(pageNum,3);
        List<User> users;
        if (!"".equals(name)){
            UserExample example=new UserExample();
            UserExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+name+"%");
             users = userMapper.selectByExample(example);
        }else{
            users=userMapper.selectByExample(null);
        }
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public String getUser(String password) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(example);
        if(users!=null&&users.size()>0){
            return "true";
        }
        return "fail";
    }

    @Override
    public Integer updateUser(User user) {

        return userMapper.updateByPrimaryKeySelective(user);

    }
}
